{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Converting a count matrix in CSV format to Sparse h5ad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (c) 2020, NVIDIA CORPORATION. \n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\") \n",
    "you may not use this file except in compliance with the License. \n",
    "You may obtain a copy of the License at \n",
    "\n",
    "     http://www.apache.org/licenses/LICENSE-2.0 \n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software \n",
    "distributed under the License is distributed on an \"AS IS\" BASIS, \n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \n",
    "See the License for the specific language governing permissions and \n",
    "limitations under the License."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scanpy as sc\n",
    "import scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_dense_csv_to_sparse_h5ad(input_file, output_file=None):\n",
    "    \n",
    "    if output_file is None:\n",
    "        output_file = input_file + \".sparse.h5ad\"\n",
    "\n",
    "    adata = sc.read_csv(input_file)\n",
    "    adata.X = scipy.sparse.csr_matrix(adata.X)\n",
    "    adata = adata.T\n",
    "    \n",
    "    adata.write(output_file)\n",
    "    return adata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CSV file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_file = \"krasnow_hlca_10x_UMIs.csv\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Convert CSV to sparse form and save"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4min 27s, sys: 9.02 s, total: 4min 36s\n",
      "Wall time: 5min 4s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 65662 × 26485"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "convert_dense_csv_to_sparse_h5ad(csv_file, output_file=\"krasnow_hlca_10x.sparse.h5ad\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
